package club.justwill.single.assembler;

import club.justwill.single.bean.OrderItemBean;
import club.justwill.single.bean.OrderItemResp;
import club.justwill.single.bean.OrdersResp;
import club.justwill.single.persistence.jpo.OrderItem;
import club.justwill.single.persistence.jpo.Orders;
import club.justwill.single.persistence.jpo.Projects;
import club.justwill.single.persistence.jpo.Skus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luoyu on 12/31/15.
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class OrderAssemble {
    @Autowired
    private OrderItemAssemble orderItemAssemble;

    @Autowired
    private SkusAssemble skusAssemble;

    public List<OrdersResp> assembleOrderList(List<Orders> ordersList) {
        List<OrdersResp> ordersResps = new ArrayList<OrdersResp>();

        for(Orders orders:ordersList){
            OrdersResp ordersResp = assembleOrder(orders);
            ordersResps.add(ordersResp);
        }
        return ordersResps;
    }

    public OrdersResp assembleOrder(Orders orders){
        OrdersResp ordersResp = new OrdersResp();
        ordersResp.setOrderId(orders.getOrderId()+"");
        ordersResp.setName(orders.getName());
        ordersResp.setProvince(orders.getProvince());
        ordersResp.setCity(orders.getCity());
        ordersResp.setAddress(orders.getAddress());
        ordersResp.setUseComments(orders.getUseComments());
        ordersResp.setServiceComments(orders.getServiceComments());
        ordersResp.setStatus(orders.getStatus()+"");
        ordersResp.setSerialNumber(orders.getSerialNumber());
        ordersResp.setExpressCompany(orders.getExpressCompany());
        ordersResp.setExpressNumber(orders.getExpressNumber());
        ordersResp.setPhoneNumber(orders.getPhoneNumber());
        ordersResp.setCreateDate(String.valueOf(orders.getProcessTimeStamp()));
        double totalPrice = 0;
        for(OrderItem orderItem:orders.getOrderItemSet()){
            ordersResp.getOrderItemRespList().add(orderItemAssemble.assembleOrderItem(orderItem));
            Skus skus = orderItem.getSkus();
            if (skus != null) {
                ordersResp.getSkusRespList().add(skusAssemble.assembleSkus(skus));
                totalPrice += skus.getUnitprice()*orderItem.getUnits();
            }

        }
        ordersResp.setTotlalPrice(totalPrice+"");
        return ordersResp;
    }


  /*  public OrdersResp assembleOrder(Orders orders,List<OrderItem> orderItemList, List<Skus> skusList) {
        OrdersResp ordersResp = new OrdersResp();
        ordersResp.setOrderId(orders.getOrderId()+"");
        ordersResp.setName(orders.getName());
        ordersResp.setPhoneNumber(orders.getPhoneNumber());
        ordersResp.setProvince(orders.getProvince());
        ordersResp.setCity(orders.getCity());
        ordersResp.setAddress(orders.getAddress());
        ordersResp.setUseComments(orders.getUseComments());
        ordersResp.setServiceComments(orders.getServiceComments());
        ordersResp.setStatus(orders.getStatus()+"");
        ordersResp.setSerialNumber(orders.getSerialNumber());
        ordersResp.setExpressCompany(orders.getExpressCompany());
        ordersResp.setExpressNumber(orders.getExpressNumber());
        List<OrderItemResp> list = new ArrayList<OrderItemResp>();
        double totalPrice = 0;
        for(OrderItem orderItem:orderItemList){
            OrderItemResp orderItemResp = new OrderItemResp();
            orderItemResp.setOrderId(orderItem.getOrderId());
            orderItemResp.setSkuId(orderItem.getSkuId());
            for(Skus sku:skusList){
                if(sku.getSkusId() == orderItem.getSkuId()){
                    orderItemResp.setUnitprice(String.valueOf(sku.getUnitprice()));
                    orderItemResp.setRealPrice(String.valueOf(sku.getRealPrice()));
                    orderItemResp.setSkusUrl(sku.getSkuUrl());
                    totalPrice += sku.getUnitprice()*orderItem.getUnits();
                }

            }
            orderItemResp.setUnits(orderItem.getUnits());
            list.add(orderItemResp);
        }
        ordersResp.setOrderItemRespList(list);
        ordersResp.setTotlalPrice(totalPrice + "");
        return ordersResp;
    }*/

    /*public OrdersResp assembleOrder(Orders orders,List<OrderItem> orderItemList, List<Skus> skusList,Projects projects) {
        OrdersResp ordersResp = assembleOrder(orders,orderItemList,skusList);
        ordersResp.setCover(projects.getCover());
        return ordersResp;
    }*/
}
